using System;
using System.IO;

public static class StringExtension
{
	public static string GetEndingCharacters(this string source, int tail_length)
	{
		if (tail_length >= source.Length)
		{
			return source;
		}
		return source.Substring(source.Length - tail_length);
	}

	public static string GetStartingCharacters(this string source, int length)
	{
		if (length >= source.Length)
		{
			return source;
		}
		return source.Substring(0, length);
	}

	public static string Cypher(this string s, int x)
	{
		char[] array = s.ToCharArray();
		for (int i = 0; i < array.Length; i++)
		{
			array[i] = (char)(array[i] ^ x);
		}
		return new string(array);
	}

	public static string FirstLetterToUpper(this string str)
	{
		if (str == null)
		{
			return null;
		}
		if (str.Length > 1)
		{
			return char.ToUpper(str[0]) + str.Substring(1);
		}
		return str.ToUpper();
	}

	public static string AppendTimeStamp(this string fileName)
	{
		return Path.GetFileNameWithoutExtension(fileName) + DateTime.Now.ToString("yyyyMMddHHmmssfff") + Path.GetExtension(fileName);
	}

	public static TEnum ToEnum<TEnum>(this string strEnumValue, TEnum defaultValue)
	{
		try
		{
			if (!Enum.IsDefined(typeof(TEnum), strEnumValue))
			{
				return defaultValue;
			}
			return (TEnum)Enum.Parse(typeof(TEnum), strEnumValue);
		}
		catch (Exception)
		{
			return defaultValue;
		}
	}
}
